package com.fand.platform.userCenter.controller;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fand.context.mapper.JsonMapper;
import com.fand.dto.PaginationDto;
import com.fand.platform.userCenter.dto.UserDto;
import com.fand.platform.userCenter.dto.UserLoInDto;
import com.fand.platform.userCenter.dto.UserReDto;
import com.fand.platform.configuration.service.ConfigurationService;
import com.fand.platform.userCenter.validator.UserValidator;
import com.fand.platform.userCenter.validator.UserLoInValidator;
import com.fand.platform.userCenter.validator.UserReValidator;
import com.fand.platform.userCenter.vo.UserVo;
import com.fand.platform.userCenter.service.UserCenterService;
import com.google.common.collect.Maps;

/**
 * 用户
 *
 * @author yaoHT
 */
@Controller
public class UserController {
	@Qualifier(UserCenterService.BEAN_ID)
	@Autowired(required = false)
	private UserCenterService userCenterService;

	@Qualifier(UserValidator.BEAN_ID)
	@Autowired(required = false)
	private Validator userValidator;

	@Qualifier(UserLoInValidator.BEAN_ID)
	@Autowired(required = false)
	private Validator userLoInValidator;

	@Qualifier(UserReValidator.BEAN_ID)
	@Autowired(required = false)
	private Validator userReValidator;

	@RequestMapping(value = { "/admin/userCenter/user/user/delete.json" }, method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> adminUserDelete(final UserVo userVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.userCenterService.getUserService().deleteUserByIds(userVo.getUser());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return jsonMap;
	}

	@RequestMapping(value = { "/admin/userCenter/user/user/edit.html" }, method = RequestMethod.GET)
	public String adminUserEdit(final UserVo userVo, final Model model) {
		final UserDto user = this.userCenterService.getUserService().
				getUserById(userVo.getUser());

		model.addAttribute("user", user);

		return "/admin/userCenter/user/user/edit";
	}

	@RequestMapping(value = { "/admin/userCenter/user/user.html", "/admin/userCenter/user/user/index.html" })
	public String adminUserIndex() {
		return "/admin/userCenter/user/user/index";
	}

	@RequestMapping(value = { "/admin/userCenter/user/user/indexPagination.json" })
	public @ResponseBody
	Map<String, Object> adminUserIndexPagination(final UserVo userVo) {
		final PaginationDto<Map<String, Object>> userPagination = this.userCenterService.getUserService()
				.paginationUserBy1ForMapList(userVo.getUser());

		final Map<String, Object> userPaginationMap = Maps.newHashMap();

		userPaginationMap.put("total", userPagination.getTotalSize());
		userPaginationMap.put("rows", userPagination.getDataList());

		return userPaginationMap;
	}

	@RequestMapping(value = { "/admin/userCenter/user/user/new.html" }, method = RequestMethod.GET)
	public String adminUserNew(final UserVo userVo, final Model model) {
		model.addAttribute("user", userVo.getUser());

		return "/admin/userCenter/user/user/new";
	}

	@RequestMapping(value = { "/admin/userCenter/user/user/save.txt" }, method = RequestMethod.POST)
	public @ResponseBody
	String adminUserSave(final UserVo userVo, BindingResult bindingResult) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.userValidator.validate(userVo.getUser(), bindingResult);

			if (bindingResult.hasErrors()) {
				String message = StringUtils.EMPTY;

				for (final ObjectError objectError : bindingResult.getAllErrors()) {
					message += objectError.getDefaultMessage();
				}

				throw new Exception(message);
			}

			this.userCenterService.getUserService().saveUserByIdOrCodeForUser(userVo.getUser());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return JsonMapper.nonEmptyMapper().toJson(jsonMap);
	}

	@RequestMapping(value = { "/admin/userCenter/user/user/validationBy1.json" })
	public @ResponseBody
	Boolean adminUserValidationBy1(final UserVo userVo) {
		try {
			final int count = this.userCenterService.getUserService().readUserByCodeNqIdForInteger(
					userVo.getUser());

			return (count < 1);
		} catch (final Exception e) {
		}

		return false;
	}

	@RequestMapping(value = { "/admin/userCenter/user/user/view.html" }, method = RequestMethod.GET)
	public String adminUserView(final UserVo userVo, final Model model) {
		final UserDto user = this.userCenterService.getUserService().
					getUserById(userVo.getUser());

		model.addAttribute("user", user);

		return "/admin/userCenter/user/user/view";
	}

	@RequestMapping(value = { "/admin/userCenter/user/userLoIn/delete.json" }, method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> adminUserLoInDelete(final UserVo userVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.userCenterService.getUserService().deleteUserLoInByIds(userVo.getUserLoIn());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return jsonMap;
	}

	@RequestMapping(value = { "/admin/userCenter/user/userLoIn/edit.html" }, method = RequestMethod.GET)
	public String adminUserLoInEdit(final UserVo userVo, final Model model) {
		final UserLoInDto userLoIn = this.userCenterService.getUserService().
				getUserLoInById(userVo.getUserLoIn());

		model.addAttribute("userLoIn", userLoIn);

		return "/admin/userCenter/user/userLoIn/edit";
	}

	@RequestMapping(value = { "/admin/userCenter/user/userLoIn.html", "/admin/userCenter/user/userLoIn/index.html" })
	public String adminUserLoInIndex() {
		return "/admin/userCenter/user/userLoIn/index";
	}

	@RequestMapping(value = { "/admin/userCenter/user/userLoIn/indexPagination.json" })
	public @ResponseBody
	Map<String, Object> adminUserLoInIndexPagination(final UserVo userVo) {
		final PaginationDto<Map<String, Object>> userLoInPagination = this.userCenterService.getUserService()
				.paginationUserLoInBy1ForMapList(userVo.getUserLoIn());

		final Map<String, Object> userLoInPaginationMap = Maps.newHashMap();

		userLoInPaginationMap.put("total", userLoInPagination.getTotalSize());
		userLoInPaginationMap.put("rows", userLoInPagination.getDataList());

		return userLoInPaginationMap;
	}

	@RequestMapping(value = { "/admin/userCenter/user/userLoIn/new.html" }, method = RequestMethod.GET)
	public String adminUserLoInNew(final UserVo userVo, final Model model) {
		model.addAttribute("userLoIn", userVo.getUserLoIn());

		return "/admin/userCenter/user/userLoIn/new";
	}

	@RequestMapping(value = { "/admin/userCenter/user/userLoIn/save.txt" }, method = RequestMethod.POST)
	public @ResponseBody
	String adminUserLoInSave(final UserVo userVo, BindingResult bindingResult) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.userLoInValidator.validate(userVo.getUserLoIn(), bindingResult);

			if (bindingResult.hasErrors()) {
				String message = StringUtils.EMPTY;

				for (final ObjectError objectError : bindingResult.getAllErrors()) {
					message += objectError.getDefaultMessage();
				}

				throw new Exception(message);
			}

			this.userCenterService.getUserService().saveUserLoInByIdOrCodeForUserLoIn(userVo.getUserLoIn());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return JsonMapper.nonEmptyMapper().toJson(jsonMap);
	}

	@RequestMapping(value = { "/admin/userCenter/user/userLoIn/validationBy1.json" })
	public @ResponseBody
	Boolean adminUserLoInValidationBy1(final UserVo userVo) {
		try {
			final int count = this.userCenterService.getUserService().readUserLoInByCodeNqIdForInteger(
					userVo.getUserLoIn());

			return (count < 1);
		} catch (final Exception e) {
		}

		return false;
	}

	@RequestMapping(value = { "/admin/userCenter/user/userLoIn/view.html" }, method = RequestMethod.GET)
	public String adminUserLoInView(final UserVo userVo, final Model model) {
		final UserLoInDto userLoIn = this.userCenterService.getUserService().
					getUserLoInById(userVo.getUserLoIn());

		model.addAttribute("userLoIn", userLoIn);

		return "/admin/userCenter/user/userLoIn/view";
	}

	@RequestMapping(value = { "/admin/userCenter/user/userRe/delete.json" }, method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> adminUserReDelete(final UserVo userVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.userCenterService.getUserService().deleteUserReByIds(userVo.getUserRe());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return jsonMap;
	}

	@RequestMapping(value = { "/admin/userCenter/user/userRe/edit.html" }, method = RequestMethod.GET)
	public String adminUserReEdit(final UserVo userVo, final Model model) {
		final UserReDto userRe = this.userCenterService.getUserService().
				getUserReById(userVo.getUserRe());

		model.addAttribute("userRe", userRe);

		return "/admin/userCenter/user/userRe/edit";
	}

	@RequestMapping(value = { "/admin/userCenter/user/userRe.html", "/admin/userCenter/user/userRe/index.html" })
	public String adminUserReIndex() {
		return "/admin/userCenter/user/userRe/index";
	}

	@RequestMapping(value = { "/admin/userCenter/user/userRe/indexPagination.json" })
	public @ResponseBody
	Map<String, Object> adminUserReIndexPagination(final UserVo userVo) {
		final PaginationDto<Map<String, Object>> userRePagination = this.userCenterService.getUserService()
				.paginationUserReBy1ForMapList(userVo.getUserRe());

		final Map<String, Object> userRePaginationMap = Maps.newHashMap();

		userRePaginationMap.put("total", userRePagination.getTotalSize());
		userRePaginationMap.put("rows", userRePagination.getDataList());

		return userRePaginationMap;
	}

	@RequestMapping(value = { "/admin/userCenter/user/userRe/new.html" }, method = RequestMethod.GET)
	public String adminUserReNew(final UserVo userVo, final Model model) {
		model.addAttribute("userRe", userVo.getUserRe());

		return "/admin/userCenter/user/userRe/new";
	}

	@RequestMapping(value = { "/admin/userCenter/user/userRe/save.txt" }, method = RequestMethod.POST)
	public @ResponseBody
	String adminUserReSave(final UserVo userVo, BindingResult bindingResult) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.userReValidator.validate(userVo.getUserRe(), bindingResult);

			if (bindingResult.hasErrors()) {
				String message = StringUtils.EMPTY;

				for (final ObjectError objectError : bindingResult.getAllErrors()) {
					message += objectError.getDefaultMessage();
				}

				throw new Exception(message);
			}

			this.userCenterService.getUserService().saveUserReByIdOrCodeForUserRe(userVo.getUserRe());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return JsonMapper.nonEmptyMapper().toJson(jsonMap);
	}

	@RequestMapping(value = { "/admin/userCenter/user/userRe/validationBy1.json" })
	public @ResponseBody
	Boolean adminUserReValidationBy1(final UserVo userVo) {
		try {
			final int count = this.userCenterService.getUserService().readUserReByCodeNqIdForInteger(
					userVo.getUserRe());

			return (count < 1);
		} catch (final Exception e) {
		}

		return false;
	}

	@RequestMapping(value = { "/admin/userCenter/user/userRe/view.html" }, method = RequestMethod.GET)
	public String adminUserReView(final UserVo userVo, final Model model) {
		final UserReDto userRe = this.userCenterService.getUserService().
					getUserReById(userVo.getUserRe());

		model.addAttribute("userRe", userRe);

		return "/admin/userCenter/user/userRe/view";
	}
}
